package keyfun.utils 
{
	import flash.text.TextField;
	import flash.text.TextFormat;
	/**
	 * ...
	 * @author Key Hui
	 */
	public class TextUtil 
	{
		
		public static function removeWhiteSpace(str:String):String
		{
			str = str.replace("\r", "").replace("\n", "");
			var tf:TextField = new TextField();
			
			tf.condenseWhite = true;
			tf.htmlText = str;
			str = tf.text;
			
			return str;
		}
		
		public static function removeLineBreak(str:String):String
		{
			str = str.replace("\r", "");
			str = str.replace("\n", " ");
			return str;
		}
		
		public static function removeLineBreak_r(str:String):String
		{
			str = str.replace("\r", "");
			return str;
		}
		
		public static function replaceLineBreak(str:String, replaceTxt:String="\n"):String
		{
			var i:int;
			str = str.replace("\r", "");
			var tf:TextField = new TextField();
			tf.text = str;
			
			var newStr:String = "";
			var lineArr:Vector.<String> = new Vector.<String>();
			for (i = 0; i < tf.numLines; i++)
			{
				var tmpStr:String = tf.getLineText(i);
				if (tmpStr.charCodeAt(tmpStr.length - 1) == 13)
				{
					tmpStr = tmpStr.substr(0, tmpStr.length - 1);
				}
				//trace("line-" + i + ":" + tmpStr + ", n:" + tmpStr.length);
				if (tmpStr.length > 1)
				{
					lineArr.push(tmpStr);
				}
			}
			
			for (i = 0; i < lineArr.length; i++)
			{
				newStr += lineArr[i];
				if (i < lineArr.length - 1)
				{
					newStr += replaceTxt;
				}
			}
			//trace(newStr);
			return newStr;
		}
		
		public static function updateTextFontSize(txt:TextField, size:Number):void
		{
			var tmpFormat:TextFormat;
			tmpFormat = txt.getTextFormat();
			tmpFormat.size = size;
			txt.setTextFormat(tmpFormat);
			txt.defaultTextFormat = tmpFormat;
		}
		
	}

}